﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DSS_XNA.Engine.Error;

namespace DSS_XNA.Engine.Script.Variables.VariableObjects
{
    public class Float : IVariable
    {
        #region IVariable Members

        public Type DotNetSimilarType
        {
            get { return typeof(float); }
        }

        public VariableType Type
        {
            get { return VariableType.Float; }
        }

        public string TypeName
        {
            get { return "float"; }
        }

        public object Value
        {
            get
            {
                return this._value;
            }
            set
            {
                this._value = (float)Convert.ToDouble(value);
            }
        }
        private float _value = default(float);

        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }
        private string _name = global::System.String.Empty;

        public VariableScope Scope
        {
            get { return _scope; }
            set { _scope = value; }
        }
        private VariableScope _scope = VariableScope.Local;

        public object DefaultValue
        {
            get { return default(float); }
        }

        public void Reset()
        {
            this.Value = this.DefaultValue;
        }

        public IVariable Add(IVariable var)
        {
            if (var == null)
                throw new DSSException("'var' is null.");

            return this;
        }

        public IVariable Subtract(IVariable var)
        {
            if (var == null)
                throw new DSSException("'var' is null.");

            return this;
        }

        public IVariable Multiply(IVariable var)
        {
            if (var == null)
                throw new DSSException("'var' is null.");

            return this;
        }

        public IVariable Divide(IVariable var)
        {
            if (var == null)
                throw new DSSException("'var' is null.");

            return this;
        }

        public IVariable Add(object var)
        {
            if (var == null)
                throw new DSSException("'var' is null.");

            return this;
        }

        public IVariable Subtract(object var)
        {
            if (var == null)
                throw new DSSException("'var' is null.");

            return this;
        }

        public IVariable Multiply(object var)
        {
            if (var == null)
                throw new DSSException("'var' is null.");

            return this;
        }

        public IVariable Divide(object var)
        {
            if (var == null)
                throw new DSSException("'var' is null.");

            return this;
        }

        public VariableCompareResult Compare(IVariable var)
        {
            if (var == null)
                throw new DSSException("'var' is null.");

            int compareResult = 0;
            VariableCompareResult res = VariableCompareResult.NotComparable;

            try
            {
                switch (var.Type)
                {
                    case VariableType.Object:
                        compareResult = this.Value.Equals(var.Value) ? 0 : 1;
                        break;
                    case VariableType.Integer:
                        compareResult = ((int)this.Value).CompareTo(var.Value);
                        break;
                    case VariableType.Float:
                        compareResult = ((float)this.Value).CompareTo(var.Value);
                        break;
                    case VariableType.String:
                        string s = this.Value.ToString();
                        compareResult = s.CompareTo(var.Value);
                        break;
                    default:
                        throw new DSSException(global::System.String.Format("'{0}' is an unknown variable type",
                            var.Type));
                }
            }
            catch
            {
                res = VariableCompareResult.NotComparable;
            }

            if (compareResult == 0)
                res = VariableCompareResult.Equal;
            else if (compareResult > 0)
                res = VariableCompareResult.GreaterThan;
            else if (compareResult < 0)
                res = VariableCompareResult.LessThan;
            else
                res = VariableCompareResult.NotComparable;

            return res;
        }

        #endregion
    }
}
